package com.bw.permission.config;

import com.alibaba.fastjson.JSON;
import com.bw.permission.utils.JwtUtils;
import com.bw.permission.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    JwtUtils jwtUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        log.info("requestURI:{}",requestURI);
        //返回的登陆名称为空，可以认为是登陆过期了
        String curUsername = jwtUtils.getCurUsername();
        String token = request.getHeader("token");
        if (curUsername == null && token!=null) {
//            throw new BizException(400, "未登录");
            return false;
        }
        return true;
    }
}
